<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="仓库" prop="warehouseId">
        <el-input v-model="queryParams.warehouseId" placeholder="请输入仓库" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="入库单号" prop="warehousingRequisitionOrder">
        <el-input v-model="queryParams.warehousingRequisitionOrder" placeholder="请输入入库单号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="存货名称" prop="qualityName">
        <el-input v-model="queryParams.qualityName" placeholder="请输入存货名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="供应商" prop="supplierId">
        <el-input v-model="queryParams.supplierId" placeholder="请输入供应商" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="质检日期" prop="qualityDate">
        <el-date-picker clearable v-model="queryParams.qualityDate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择质检日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="质检人员" prop="qualityPerson">
        <el-input v-model="queryParams.qualityPerson" placeholder="请输入质检人员" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['quality:quality:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['quality:quality:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['quality:quality:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['quality:quality:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="qualityList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <router-link :to="'/supplier-table/supplier/' + scope.row.supplierId" class="link-type">
                        <el-button size="mini" type="warning" icon="el-icon-s-goods"
                        v-hasPermi="['supplier:supplier:list']">质检状态</el-button>
                    </router-link>
                </template>
            </el-table-column> -->
      <el-table-column label="质检" align="center" prop="qualityId" />
      <!-- <el-table-column label="入库id" align="center" prop="warehousingRequisitionId" /> -->
      <el-table-column label="入库单号" align="center" prop="warehousingRequisitionOrder" />
      <el-table-column label="业务员" align="center" prop="qualityName" />

      <el-table-column label="供应商" align="center" prop="supplierId" />
      <el-table-column label="质检类型" align="center" prop="qualityType">
        <template slot-scope="scope">
          <span v-if="scope.row.qualityType == 0">
            <el-tag type="success">原料质检</el-tag>
          </span>
          <span v-if="scope.row.qualityType == 1">
            <el-tag type="success">成品质检</el-tag>
          </span>

        </template>
      </el-table-column>
      <el-table-column label="质检状态" align="center" prop="qualityStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.qualityStatus == 0">
            <el-tag type="danger">未质检</el-tag>
          </span>
          <span v-if="scope.row.qualityStatus == 1">
            <el-tag type="success">质检完成</el-tag>
          </span>
         

        </template>
      </el-table-column>
      <el-table-column label="质检日期" align="center" prop="qualityDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.qualityDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="质检人员" align="center" prop="qualityPerson" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleRows(scope.row)"
            v-hasPermi="['quality:quality:edit']">质检报告</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 质检报告对话框 -->
    <el-dialog :title="title" :visible.sync="openRow" width="960px" append-to-body>

      <el-form :model="queryRow" ref="queryRow" size="small" :inline="true" v-show="showSearch" label-width="68px">




      </el-form>
      <el-table v-loading="loading" :data="rawList" @selection-change="handleChange" border style="width: 100%"
        @cell-click="cellclick" :cell-style="tableCellStyle">

        <el-table-column type="selection" width="55" align="center" prop="center" />
        <!-- <el-table-column label="质检详情" align="center" prop="qualityRawId" />
          <el-table-column label="质检id" align="center" prop="qualityId" /> -->
        <el-table-column label="质检名称" align="center" prop="qualityName" />
        <el-table-column label="规格型号" align="center" prop="qualitySpeion" />
        <el-table-column label="数量" align="center" prop="qualityNum" />
        <el-table-column label="外观质量检查" align="center" prop="qualityAppearance">
          <template slot-scope="scope">
            <span v-if="scope.row.qualityAppearance == 0">
              <div></div>
            </span>
            <span v-if="scope.row.qualityAppearance == 1">
              <div>合格</div>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="尺寸大小检查" align="center" prop="qualityDimensiona">
          <template slot-scope="scope">
            <span v-if="scope.row.qualityDimensiona == 0">
              <div></div>
            </span>
            <span v-if="scope.row.qualityDimensiona == 1">
              <div>合格</div>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="强度和耐性检查" align="center" prop="qualityStrength">
          <template slot-scope="scope">
            <span v-if="scope.row.qualityStrength == 0">
              <div></div>
            </span>
            <span v-if="scope.row.qualityStrength == 1">
              <div>合格</div>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="织物结构检查" align="center" prop="qualityFabric">
          <template slot-scope="scope">
            <span v-if="scope.row.qualityFabric == 0">
              <div></div>
            </span>
            <span v-if="scope.row.qualityFabric == 1">
              <div>合格</div>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="成分检查" align="center" prop="qualityComponent">
          <template slot-scope="scope">
            <span v-if="scope.row.qualityComponent == 0">
              <div></div>
            </span>
            <span v-if="scope.row.qualityComponent == 1">
              <div>合格</div>
            </span>
          </template>
        </el-table-column>

        <el-table-column label="化学物质检查" align="center" prop="qualityChemical">
          <template slot-scope="scope">
            <span v-if="scope.row.qualityChemical == 0">
              <div></div>
            </span>
            <span v-if="scope.row.qualityChemical == 1">
              <div>合格</div>
            </span>
          </template>
        </el-table-column>

        <el-table-column label="质检数量" align="center" prop="qualityRawNum">
          <template slot-scope="scope">
            <el-input v-model="scope.row.qualityRawNum" :min="1" :max="1000" label="质检数量"></el-input>

          </template>
        </el-table-column>
        <el-table-column label="不合格数量" align="center" prop="qualityUnqualifiedNum">
          <template slot-scope="scope">
            <el-input v-model="scope.row.qualityUnqualifiedNum" :min="1" :max="1000" label="质检数量"></el-input>

          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="todate">
          <template slot-scope="scope">
            <span v-if="scope.row.qualityQualifiedSex ==0">
              <el-button size="mini" type="primary" @click="quNum(scope.row)">确定</el-button>
          </span>          
            <span v-if="scope.row.qualityQualifiedSex > 30">
              <el-tag type="danger">退货</el-tag>
          </span>
          </template>
        </el-table-column>
      </el-table>


      <div slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" icon="el-icon-circle-check" @click="Warehouse()">选择仓库</el-button>
        <el-button type="primary" >退 货</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>

    </el-dialog>

    <!-- 选择对话框 -->
    <el-dialog :title="title" :visible.sync="openWarehouse" width="960x" append-to-body>
      <el-row>
        <el-col :span="1.5">
          <el-button type="info" plain icon="el-icon-sort" size="mini" @click="toggleExpandAll">展开/折叠</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
      <el-table v-if="refreshTable" v-loading="loading" :data="warehouseDeptList" row-key="warehouseDeptId"
        :default-expand-all="isExpandAll" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">

        <el-table-column label="仓库名称" align="center" prop="warehouseDeptName" />


        <el-table-column label="仓库状态" align="center" prop="warehouseDeptStatus">
          <template slot-scope="scope">
            <span v-if="scope.row.warehouseDeptStatus == 0">
              <el-tag type="success">正常</el-tag>
            </span>
            <span v-if="scope.row.warehouseDeptStatus == 1">
              <el-tag type="danger">占用</el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <span v-if="scope.row.warehouseDeptStatus == 0">
              <span v-if="scope.row.warehouseDeptOrderNum == 3">
                <el-button size="mini" type="text" icon="el-icon-circle-check" @click="submitWarehouse(scope.row)"
              >选择</el-button>
              </span>
            </span>
          </template>
        </el-table-column>
      </el-table>

    </el-dialog>

    <!-- 添加或修改质检管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="仓库" prop="warehouseId">
          <el-input v-model="form.warehouseId" placeholder="请输入仓库" />
        </el-form-item>
        <el-form-item label="入库id" prop="warehousingRequisitionId">
          <el-input v-model="form.warehousingRequisitionId" placeholder="请输入入库id" />
        </el-form-item>
        <el-form-item label="入库单号" prop="warehousingRequisitionOrder">
          <el-input v-model="form.warehousingRequisitionOrder" placeholder="请输入入库单号" />
        </el-form-item>
        <el-form-item label="存货名称" prop="qualityName">
          <el-input v-model="form.qualityName" placeholder="请输入存货名称" />
        </el-form-item>
        <el-form-item label="供应商" prop="supplierId">
          <el-input v-model="form.supplierId" placeholder="请输入供应商" />
        </el-form-item>
        <el-form-item label="质检日期" prop="qualityDate">
          <el-date-picker clearable v-model="form.qualityDate" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择质检日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="质检人员" prop="qualityPerson">
          <el-input v-model="form.qualityPerson" placeholder="请输入质检人员" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
<script>
import { auditQuality,listQuality, getQuality, delQuality, addQuality, updateQuality } from "@/api/quality/quality";
import { listRaw, getRaw, delRaw, addRaw, updateRaw } from "@/api/quality/raw";
import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  import { listWarehouseDept } from "@/api/warehouseDept/warehouseDept";
export default {
  name: "Quality",
  data() {
    return {
      quaSex:"",//合格率
           // 是否展开，默认全部展开
           isExpandAll: false,
      formWarehouse:{},
    // 仓库分类树选项
    warehouseDeptOptions: [],
       // 重新渲染表格状态
       refreshTable: true,
      quId: {},
      row: '',
      column: '',
      openRow: false,
      rawList: [
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 质检管理表格数据
      qualityList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        warehouseId: null,
        warehousingRequisitionId: null,
        warehousingRequisitionOrder: null,
        qualityName: null,
        supplierId: null,
        qualityType: null,
        qualityStatus: null,
        qualityDate: null,
        qualityPerson: null,
      },
      quaNum: {},
      // 表单参数
      form: {},
      queryRow: {},
      // 表单校验
      rules: {
      },
      orderList:[],
      qualityData:{},
      openWarehouse: false,
      rows:[],
      qualiType: {},
      warehouseDeptList:[],
    };
  },
  created() {
    this.getList();
  },
  methods: {
     
      
     /** 查询仓库分类列表 */
     getWarehouseList() {
        this.loading = true;
        listWarehouseDept(this.queryParams).then(response => {
          this.warehouseDeptList = this.handleTree(response.data, "warehouseDeptId", "warehouseDeptParentId");
          this.loading = false;
        });
      },
      /** 转换仓库分类数据结构 */
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.warehouseDeptId,
          label: node.warehouseDeptAncestors,
          children: node.children
        };
      },
    /** 展开/折叠操作 */
    toggleExpandAll() {
        this.refreshTable = false;
        this.isExpandAll = !this.isExpandAll;
        this.$nextTick(() => {
          this.refreshTable = true;
        });
      },
      //选择仓库
     submitWarehouse(row){
      this.qualityData.qualityId=this.queryRow.qualityId 
      this.qualityData.warehouseDeptId=row.warehouseDeptId;
      this.qualityData.orderList=this.rows
      this.qualityData.warehousingRequisitionOrder=this.queryRow.warehousingRequisitionOrder
      this.qualityData.qualityName=this.queryRow.qualityName
      console.log(this.qualityData);
      auditQuality(this.qualityData).then(response => {
        this.$modal.msgSuccess("审核操作成功");
        this.openRow = false;
        this.openWarehouse = false;
        this.getList();
    })
  },
    Warehouse(){
      this.title="选择仓库"
      this.openWarehouse=true;
      this.getWarehouseList();
    },
    statusCellStyle({ row, rowIndex, column }) {
      if (row[column.property] === 0) {
        return { background: "white" };
      } else if (row[column.property] === 1) {
        return { background: "green" };
      }

    },
    quNum(row) {
      this.quaNum.qualityRawId = row.qualityRawId
      this.quaNum.qualityRawNum = row.qualityRawNum;
      this.quaNum.qualityUnqualifiedNum = row.qualityUnqualifiedNum;
      const quaSex=this.quaNum.qualityUnqualifiedNum/this.quaNum.qualityRawNum*100//不合格率
      console.log(quaSex)
      this.quaNum.qualityQualifiedSex=quaSex
      updateRaw(this.quaNum).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.RowList();
      })
     
     
      
    },


    cellclick(row, column, cell, event) {
      this.row = row
      this.column = column
      this.quId.qualityRawId = this.row.qualityRawId
      this.quId.name = this.column.property

    },
    tableCellStyle(row, rowIndex, column) {

      if (this.row && this.columnName) { }


      if (this.row === row.row && this.column === row.column) {
        if (this.column.property === "qualityRawNum" || this.column.property === "qualityUnqualifiedNum" || this.column.property === "todate"  ||this.column.property ==="center") {

        } else {
          updateRaw(this.quId).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.RowList();
          })
        }
        return 'background-color:#00FF00;'
      } else {

      }
    },


    
    /** 查询原料质检列表 */
    RowList() {
      this.loading = true;
      listRaw(this.queryRow).then(response => {
        this.rawList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleChange(selection) {
      this.rows = selection.map(item => item)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleRows(row) {
      this.openRow = true;
      this.queryRow.qualityId = row.qualityId
      this.queryRow.warehousingRequisitionOrder=row.warehousingRequisitionOrder 
      this.queryRow.qualityName = row.qualityName
      this.RowList();
      this.title = '质检报告'
    },
    /** 查询质检管理列表 */
    getList() {
      this.loading = true;
      listQuality(this.queryParams).then(response => {
        this.qualityList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        qualityId: null,
        warehouseId: null,
        warehousingRequisitionId: null,
        warehousingRequisitionOrder: null,
        qualityName: null,
        supplierId: null,
        qualityType: null,
        qualityStatus: null,
        qualityDate: null,
        qualityPerson: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.qualityId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加质检管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const qualityId = row.qualityId || this.ids
      getQuality(qualityId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改质检管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.qualityId != null) {
            updateQuality(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQuality(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const qualityIds = row.qualityId || this.ids;
      this.$modal.confirm('是否确认删除质检管理编号为"' + qualityIds + '"的数据项？').then(function () {
        return delQuality(qualityIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('quality/quality/export', {
        ...this.queryParams
      }, `quality_${new Date().getTime()}.xlsx`)
    },

  }
};
</script>
<style>
.cell-grey {
  background: #f2f2f2;
}
</style>
